package com.zut.movie.controller;


import com.zut.movie.constant.JwtClaimsConstant;
import com.zut.movie.entity.Code;
import com.zut.movie.entity.User;
import com.zut.movie.entity.Wx;
import com.zut.movie.properties.JwtProperties;
import com.zut.movie.result.Result;
import com.zut.movie.result.ResultCode;
import com.zut.movie.service.UserService;
import com.zut.movie.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public RestTemplate restTemplate;

    @Autowired
    public UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    private String openid = "";

    @PostMapping("/login")
    public ResultCode login(String code){
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Wx.APPID + "&secret=" + Wx.APPSECRET
                + "&js_code=" + code + "&grant_type=authorization_code";

        System.out.println("................"+code+".................");
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(
                MediaType.TEXT_HTML,
                MediaType.TEXT_PLAIN
        ));

        restTemplate.getMessageConverters().add(converter);

        ResponseEntity<Map> response = restTemplate.getForEntity(url,Map.class);

        openid = (String) response.getBody().get("openid");

        User user = userService.getById(openid);
        if (user==null)
        {
            //用户尚未注册
            userService.insertId(openid);
            User user2 = userService.getById(openid);
            //登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.ID, user2.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getSecretKey(),
                    jwtProperties.getTtl(),
                    claims);
            Map<String, String> resultData = new HashMap<>();
            resultData.put("token",token);
            return new ResultCode(resultData,Code.LOGIN_NOT, "登录成功!已为您注册账号");

        }
        else {
            //登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.ID, user.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getSecretKey(),
                    jwtProperties.getTtl(),
                    claims);
            //用户已经注册过
            Map<String, String> resultData = new HashMap<>();

            String userAvatarUrl = user.getUserAvatarUrl();
            resultData.put("userAvatarUrl",userAvatarUrl);

            String userName = user.getUserName();
            resultData.put("userName",userName);
            resultData.put("token",token);

            return new ResultCode(resultData,Code.LOGIN_ALREADY,"登录成功!欢迎回来");
        }
    }

    @PostMapping("/name")
    public Result<String> updateUserName(String username){
        userService.updateUserName(username);
        return Result.success();
    }
    @PostMapping("/url")
    public Result<String> updateUserUrl(String avatarUrl){
        userService.updateUserUrl(avatarUrl);
        return Result.success();
    }
}
